﻿using System;
using System.Collections;
using System.Data;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for CarUserProfileDB
/// </summary>
namespace CE.CarInsurance
{
    public class CarUserProfileDB
    {
        public Int64 Insert(CarUserProfile caruserprofile)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocUserProfileInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProfileID", caruserprofile.ProfileId);
            _sPara.Add("@Salutation", caruserprofile.Salutation);
            _sPara.Add("@FirstName", caruserprofile.FirstName == string.Empty ? string.Empty : caruserprofile.FirstName);
            _sPara.Add("@SurName", caruserprofile.SurName == string.Empty ? string.Empty : caruserprofile.SurName);
            _sPara.Add("@DOB", caruserprofile.DOB);
            _sPara.Add("@Gender", caruserprofile.Gender);
            _sPara.Add("@MaritalStatus", caruserprofile.MaritalStatus);
            _sPara.Add("@OccupationType", caruserprofile.OccupationType);
            _sPara.Add("@Occupation", caruserprofile.Occupation);
            _sPara.Add("@IDNOType", caruserprofile.IDType == string.Empty ? string.Empty : caruserprofile.IDType);
            _sPara.Add("@IDNO", caruserprofile.IDNo == string.Empty ? string.Empty : caruserprofile.IDNo);
            _sPara.Add("@HighEducation", caruserprofile.HighEducation);
            _sPara.Add("@Nationality", caruserprofile.Nationality == string.Empty ? string.Empty : caruserprofile.Nationality);
            _sPara.Add("@MontlyIncome", caruserprofile.MontlyIncome);
            _sPara.Add("@JobNature", caruserprofile.JobNature);
            _sPara.Add("@YearDriveSg", caruserprofile.YearDriveSg);
            _sPara.Add("@OtherIncome", caruserprofile.OtherIncome);
            _sPara.Add("@RefCEUserId", caruserprofile.RefCEUserId);
            _sPara.Add("@Status", caruserprofile.Status);
            _sPara.Add("@IsAcceptTC", caruserprofile.IsAcceptTC);
            _sPara.Add("@Info1", caruserprofile.Info1);
            _sPara.Add("@Info2", caruserprofile.Info2);
            _sPara.Add("@Info3", caruserprofile.Info3);
            _sPara.Add("@Info4", caruserprofile.Info4);
            _sPara.Add("@Info5", caruserprofile.Info5);
            _sPara.Add("@Info6", caruserprofile.Info6);
            _sPara.Add("@Info7", caruserprofile.Info7);
            _sPara.Add("@Info8", caruserprofile.Info8);
            _sPara.Add("@Info9", caruserprofile.Info9);
            _sPara.Add("@Info10", caruserprofile.Info10);
            _sPara.Add("@Info11", caruserprofile.Info11);
            _sPara.Add("@Info12", caruserprofile.Info12);
            _sPara.Add("@Info13", caruserprofile.Info13);
            _sPara.Add("@Info14", caruserprofile.Info14);
            _sPara.Add("@Info15", caruserprofile.Info15);
            _sPara.Add("@Info16", caruserprofile.Info16);
            _sPara.Add("@Info17", caruserprofile.Info17);
            _sPara.Add("@Info18", caruserprofile.Info18);
            _sPara.Add("@Info19", caruserprofile.Info19);
            _sPara.Add("@Info20", caruserprofile.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarUserProfile GetProfile(Int64 RefCEUserId)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocUserProfileSelectSingleItem";
            CarUserProfile _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new CarUserProfile();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private CarUserProfile FillDataRecord(DataTable myDataTable)
        {
            CarUserProfile myCarUserProfile = new CarUserProfile();
            myCarUserProfile.ProfileId = Convert.ToInt64(myDataTable.Rows[0]["ProfileID"].ToString());
            myCarUserProfile.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myCarUserProfile.Salutation = myDataTable.Rows[0]["Salutation"].ToString();

            string _firstname = myDataTable.Rows[0]["FirstName"].ToString();
            if (_firstname != string.Empty)
            {
                myCarUserProfile.FirstName = _firstname;
            }

            string _surname = myDataTable.Rows[0]["SurName"].ToString();
            if (_surname != string.Empty)
            {
                myCarUserProfile.SurName = _surname;
            }

            myCarUserProfile.DOB = Convert.ToDateTime(myDataTable.Rows[0]["DOB"].ToString());
            myCarUserProfile.Gender = Convert.ToChar(myDataTable.Rows[0]["Gender"].ToString());
            myCarUserProfile.MaritalStatus = Convert.ToInt32(myDataTable.Rows[0]["MaritalStatus"].ToString());
            myCarUserProfile.OccupationType = Convert.ToInt32(myDataTable.Rows[0]["OccupationType"].ToString());
            myCarUserProfile.Occupation = Convert.ToInt32(myDataTable.Rows[0]["Occupation"].ToString());

            string _idtype = myDataTable.Rows[0]["IDNoType"].ToString();
            if (_idtype != string.Empty)
            {
                myCarUserProfile.IDType = _idtype;
            }

            string _idno = myDataTable.Rows[0]["IDNo"].ToString();
            if (_idno != string.Empty)
            {
                myCarUserProfile.IDNo = _idno;
            }
            myCarUserProfile.HighEducation = myDataTable.Rows[0]["HighEducation"].ToString();

            string _nationality = myDataTable.Rows[0]["Nationality"].ToString();
            if (_nationality != string.Empty)
            {
                myCarUserProfile.Nationality = _nationality;
            }

            myCarUserProfile.MontlyIncome = Convert.ToSingle(myDataTable.Rows[0]["MontlyIncome"].ToString());
            myCarUserProfile.OtherIncome = Convert.ToSingle(myDataTable.Rows[0]["OtherIncome"].ToString());
            myCarUserProfile.JobNature = myDataTable.Rows[0]["JobNature"].ToString();
            myCarUserProfile.YearDriveSg = Convert.ToInt16(myDataTable.Rows[0]["YearDriveSg"].ToString());
            myCarUserProfile.Status = myDataTable.Rows[0]["Status"].ToString();
            myCarUserProfile.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            return myCarUserProfile;
        }

    }
}